﻿using System;
using System.IO;
using System.Threading.Tasks;

namespace MinimalistFoodStreet.BackEnd.Infrastructure.Tools
{
    public static class Upload
    {
        // 最大文件大小设为 5MB
        private const long MaxFileSize = 1024 * 1024 * 5;
        // 允许的文件扩展名
        private static readonly string[] AllowedFileExtensions = { ".pdf", ".docx", ".jpg", ".jpeg", ".png", ".gif" };
        // 允许的图片扩展名
        private static readonly string[] AllowedImageExtensions = { ".jpg", ".jpeg", ".png", ".gif" };

        public static async Task<string> UploadFileAsync(Microsoft.AspNetCore.Http.IFormFile file, string uploadPath,bool createDirectory=false)
        {
            if (createDirectory&&!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            if (file == null || file.Length == 0)
            {
                throw new ArgumentException("文件为空。");
            }

            if (file.Length > MaxFileSize)
            {
                throw new ArgumentException($"文件大小超出限制。最大允许大小: {MaxFileSize} 字节。");
            }

            var fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant();
            if (!Array.Exists(AllowedFileExtensions, ext => ext == fileExtension))
            {
                throw new ArgumentException($"不允许的文件类型。允许的类型: {string.Join(", ", AllowedFileExtensions)}");
            }

            var uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
            var filePath = Path.Combine(uploadPath, uniqueFileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return uniqueFileName;
        }

        public static async Task<string> UploadImageAsync(Microsoft.AspNetCore.Http.IFormFile image, string uploadPath, bool createDirectory = false)
        {
            if (createDirectory && !Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            if (image == null || image.Length == 0)
            {
                throw new ArgumentException("图片为空。");
            }

            if (image.Length > MaxFileSize)
            {
                throw new ArgumentException($"图片大小超出限制。最大允许大小: {MaxFileSize} 字节。");
            }

            var fileExtension = Path.GetExtension(image.FileName).ToLowerInvariant();
            if (!Array.Exists(AllowedImageExtensions, ext => ext == fileExtension))
            {
                throw new ArgumentException($"不允许的图片类型。允许的类型: {string.Join(", ", AllowedImageExtensions)}");
            }

            var uniqueFileName = Guid.NewGuid().ToString().Replace("-","") + fileExtension;
            var filePath = Path.Combine(uploadPath, uniqueFileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await image.CopyToAsync(stream);
            }

            return uniqueFileName;
        }

        public static async Task DeleteFileAsync(string filePath)
        {
            // 检查文件是否存在
            if (File.Exists(filePath))
            {
                try
                {
                    File.Delete(filePath);
                }
                catch (UnauthorizedAccessException)
                {
                    Console.WriteLine("没有权限删除文件");
                }
                catch (IOException e)
                {
                    Console.WriteLine($"IO异常: {e.Message}");
                }
            }
            else
            {
                Console.WriteLine("文件不存在");
            }
        }
    }
}